#include <QDebug>
#include "utils.h"
#include "os_dependence.h"





double Utils::bytesToHigherDimensions(quint64 bytes, QString & ranges)
{
    double _sz = bytes;
    if( bytes < 1024 )
        ranges = "b";
    else if( bytes < 1024*1024 )
    {
        _sz = double(bytes) / 1024;
        ranges = "Kb";
    }
    else if( bytes < (quint64)1024*1024*1024 )
    {
        _sz = double(bytes) / (1024*1024);
        ranges = "Mb";
    }
    else if( bytes < (quint64)1024*1024*1024*1024 )
    {
        _sz = double(bytes) / (1024 * 1024 * 1024);
        ranges = "Gb";
    }
    return _sz;
}



double Utils::speedToHigherDimensions(quint64 speed, QString & ranges)
{
    double _sz = speed;
    if( speed < 1024 )
        ranges = "b/s";
    else if( speed < 1024*1024 )
    {
        _sz = double(speed) / 1024;
        ranges = "kb/s";
    }
    else if( speed < (quint64)1024*1024*1024 )
    {
        _sz = double(speed) / (1024*1024);
        ranges = "mb/s";
    }
    else if( speed < (quint64)1024*1024*1024*1024 )
    {
        _sz = double(speed) / (1024 * 1024 * 1024);
        ranges = "gb/s";
    }
    return _sz;
}


QString Utils::foldeFromWay(const QString & file)
{
    if(file.endsWith("/")) {
        qWarning() << __FUNCTION__ << "file == folder way";
        return file;
    }

    int i = file.lastIndexOf("/");
    if(i < 0) return QString::null;

    return QString(file).remove(i+1, file.length() - (i+1));
}


QString Utils::nameFromWay(const QString & file)
{
    if(file.endsWith("/")) {
        qWarning() << __FUNCTION__ << "file == folder way";
        return QString::null;
    }

    int i = file.lastIndexOf("/");
    if(i < 0) return QString::null;

    return QString(file).remove(0, i+1);
}
